import axios from "axios";
import { message } from "ant-design-vue";
import { useRouter } from "vue-router";

const router = useRouter();

export function request(config) {
  const token = localStorage.getItem("token");
  const instance = axios.create({
    baseURL: "https://www.fastmock.site/mock/447c83b48931b3e2e58ac8191b8de2d3/mock",
    timeout: 5000,
    headers: {
      "Content-Type": "application/json",
      Authorization: token,
    },
  });
  // 请求拦截
  instance.interceptors.request.use(
    (config) => {
      message.loading();
      return config;
    },
    (err) => {
      console.log(err);
    }
  );
  // 响应拦截
  instance.interceptors.response.use((response) => {
    message.destroy();
    return response.data;
  }),
    (error) => {
      if (error?.response) {
        switch (error.response.status) {
          case 400:
            message.error("请求错误(400)");
            break;
          case 401:
            router.push({ name: "login" });
            break;
          case 403:
            message.error("拒绝访问(403)");
            break;
          case 404:
            message.error("请求出错(404)");
            break;
          case 408:
            message.error("请求超时(408)");
            break;
          case 500:
            message.error("服务器错误(500)");
            break;
          case 501:
            message.error("服务未实现(501)");
            break;
          case 502:
            message.error("网络错误(502)");
            break;
          case 503:
            message.error("服务不可用(503)");
            break;
          case 504:
            message.error("网络超时(504)");
            break;
          case 505:
            message.error("HTTP版本不受支持(505)");
            break;
          default:
            message.error(`连接出错(${error.response.status})!`);
        }
      } else {
        message.error("连接服务器失败!");
      }
      message.destroy();
      console.log(error);
    };
  return instance(config);
}
